import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; 



public class AuditDownload {
	/* Variables */
	// Hash table to store all traders and their respective information
	TraderHash the_traders;
	
	
	/* AuditDownload( TraderHash )
	 * This is the main constructor. It just sets the TraderHash
	 */
	public AuditDownload( TraderHash in_traders )
	{
		// Set the trader hash to store the info about traders
		the_traders = in_traders;
	}
	
	
	/* InitializeDownload()
	 * Begin the download from the Audit System and fill in all the data
	 */
	public void InitializeDownload()
	{
		// Let's rock this XML, Baby
		try {

            DocumentBuilderFactory docBuilderFactory 	= DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder 					= docBuilderFactory.newDocumentBuilder();
            Document doc 								= docBuilder.parse (new File("auditdownload.xml"));
    		
    		// normalize text representation
            doc.getDocumentElement ().normalize ();

            NodeList listOfTraders = doc.getElementsByTagName("traderinfo");
            int numberOfTraders = listOfTraders.getLength();

            // Parse by TraderInfo Object
            for( int s=0; s < numberOfTraders; s++ ){

            	// Let's do this
            	Node firstTraderNode = listOfTraders.item(s);
                if( firstTraderNode.getNodeType() == Node.ELEMENT_NODE ){


                    Element firstTraderElement = (Element)firstTraderNode;

                    // String to add to TraderHash
                    String hash_value = "";
                    
                    // Populate Trader Fields
                    Node tradernameList = (Node)firstTraderElement.getElementsByTagName("tradername").item(0).getChildNodes().item(0);
                    Node tradercashList = (Node)firstTraderElement.getElementsByTagName("cash").item(0).getChildNodes().item(0);
                    String tradername = tradernameList.getNodeValue().trim();
                    String tradercash = tradercashList.getNodeValue().trim();
                    
                    hash_value = "CASH:" + tradercash + ";";
                    
                    // Get ores for the trader
                    NodeList oreList = firstTraderElement.getElementsByTagName("ore");
                    int ore_count = oreList.getLength();
                    
                    // Update the hash value string for each ore
                    for(int ore_counter = 0; ore_counter < ore_count; ore_counter++ )
                    {
                    	// Get the items from the ore
                    	Node firstOreNode = oreList.item(ore_counter);
                        if( firstOreNode.getNodeType() == Node.ELEMENT_NODE ){
                        	Element firstOreElement = (Element)firstOreNode;
                        	
                        	Node orenameList = (Node)firstOreElement.getElementsByTagName("orename").item(0).getChildNodes().item(0);
                        	Node orequantityList = (Node)firstOreElement.getElementsByTagName("orequantity").item(0).getChildNodes().item(0);
                        	String orename = orenameList.getNodeValue().trim();
                        	String orequantity = orequantityList.getNodeValue().trim();
                        	
                        	hash_value = hash_value + orename + ":" + orequantity + ";";                        	
                        }
                    	                    	
                    }
                    
                    
                    // Create the Entry in the Trader hash
                    the_traders.createEntry(tradername, hash_value); 

                }//end of if clause
                
                


            }//end of for traderinfo loop


        }catch (SAXParseException err) {
        System.out.println ("** Parsing error" + ", line " 
             + err.getLineNumber () + ", uri " + err.getSystemId ());
        System.out.println(" " + err.getMessage ());

        }catch (SAXException e) {
        Exception x = e.getException ();
        ((x == null) ? e : x).printStackTrace ();

        }catch (Throwable t) {
        t.printStackTrace ();
        }	
		
	}
}